package com.yyy.gui.model;

import javax.swing.*;
import javax.swing.event.ListDataListener;
import java.util.List;
import com.yyy.entity.Category;
import com.yyy.service.CategoryService;

/**
 * @Author：yyy
 * @Package：com.yyy.gui.model
 * @Project：HuTuBiill
 * @name：CategoryComboBoxModel
 * @Date：2024/11/9 20:55
 * @Filename：CategoryComboBoxModel
 * 记一笔页面中的分类下拉框，通过该类实现下拉数据的更新
 */
public class CategoryComboBoxModel implements ComboBoxModel<Category> {
    public List<Category> cs=new CategoryService().list();
    public Category c;//被选中的类

    public CategoryComboBoxModel(){
        if(!cs.isEmpty())
            c=cs.get(0);
    }


    @Override
    public void setSelectedItem(Object anItem) {
        c=(Category) anItem;

    }

    @Override
    public Object getSelectedItem() {
        if(!cs.isEmpty())
            return c;
        else
            return null;
    }

    @Override
    public int getSize() {
        return cs.size();
    }

    @Override
    public Category getElementAt(int index) {
        return cs.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {

    }

    @Override
    public void removeListDataListener(ListDataListener l) {

    }
}
